/*
 * This file is part of aion-emu <aion-emu.com>.
 *
 *  aion-emu is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-emu is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-emu.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.model.gameobjects.stats.modifiers;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

import javolution.text.TextBuilder;

import com.aionemu.gameserver.model.gameobjects.stats.Stat;
import com.aionemu.gameserver.model.gameobjects.stats.StatEnum;
import com.aionemu.gameserver.skillengine.condition.Conditions;
import com.aionemu.gameserver.skillengine.model.Skill;

/**
 * @author xavier
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Modifier")
public abstract class StatModifier implements Comparable<StatModifier>
{
	protected Conditions	conditions;
	
	@XmlAttribute
	private StatEnum		name;

	@XmlAttribute
	private boolean			bonus;

	@XmlAttribute
	protected byte			priority = 0x10;

	protected static int	MODIFIER_ID	= 0;

	protected int			id;
	

	public StatModifier()
	{
		nextId();
	}

	public boolean verify(Skill env)
	{
		if (conditions == null)
			return true;
		return conditions.verify(env);
	}
	
	public boolean verify(Stat env)
	{
		if (conditions == null)
			return true;
		
		return conditions.verify(env);
	}

	protected void setStat(StatEnum stat)
	{
		this.name = stat;
	}

	protected void setBonus(boolean bonus)
	{
		this.bonus = bonus;
	}

	protected void nextId()
	{
		MODIFIER_ID = (MODIFIER_ID + 1) % Integer.MAX_VALUE;
		id = MODIFIER_ID;
	}

	public StatEnum getStat()
	{
		return name;
	}

	public boolean isBonus()
	{
		return bonus;
	}

	@Override
	public int compareTo(StatModifier o)
	{
		int result = getPriority() - o.getPriority();
		if (result == 0)
			return id-o.id;
		return result;
	}

	@Override
	public boolean equals(Object o)
	{
		boolean result = (o != null);
		result = (result) && (o instanceof StatModifier);
		result = (result) && (((StatModifier) o).id == id);
		return result;
	}

	@Override
	public int hashCode()
	{
		return id;
	}

	@Override
	public String toString()
	{
		TextBuilder tb = TextBuilder.newInstance();

		tb.append(this.getClass().getSimpleName() + ",");
		tb.append("i:" + id + ",");
		tb.append("s:" + name + ",");
		tb.append("b:" + bonus);

		String toString = tb.toString();
		TextBuilder.recycle(tb);

		return toString;
	}

	public abstract void apply(Stat stat);

	public byte getPriority()
	{
		return priority;
	}


	/**
	 * @param priority
	 */
	public void setPriority(byte priority)
	{
		this.priority = priority;
	}

	/**
	 * @return Returns the conditions.
	 */
	public Conditions getConditions()
	{
		return conditions;
	}
}
